﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using MiniWeb.Core.Dtos;
using MiniWeb.Core.Entities;
using MiniWeb.Core.Enums;
using MiniWeb.Task;
using MiniWeb.Web.Controllers.Admin.NewsManager.ViewModels;
using WFramework.Extensions;
using WFramework.Mappers;
using WFramework.MvcPager;

namespace MiniWeb.Web.Controllers.Admin.NewsManager
{

    public class NewsManagerController : BaseController
    {
        private readonly NewsTask _newsTask = new NewsTask();
        private readonly NewsTypeTask _newsTypeTask = new NewsTypeTask();

        public ActionResult List(int? roottypeId, int? newstypeID,bool? isHot, string newstitle, int pageIndex = 1, int pageSize = 20)
        {
            var pagedList = _newsTask.GetPagedList(roottypeId,newstypeID,isHot, newstitle, pageIndex, pageSize);

            ViewData["newtype"] = NewsTypeEnum.入藏指南.ToSelectListForAll();
            ViewData["roottypelist"] = RootNewsTypeEnum.商城咨询.ToSelectListForAll();
            var model = new PagedList<NewsDto>(pagedList.ToList(), pageIndex, pageSize, pagedList.TotalItemCount);
            ViewData["ishotlist"] = new List<SelectListItem>
                                        {
                                            new SelectListItem{Selected = true,Text = "请选择",Value = ""},
                                            new SelectListItem{Selected = true,Text = "是",Value = "1"},
                                            new SelectListItem{Selected = true,Text = "否",Value = "0"},
                                        };
            return View(model);
        }


        public ActionResult Save(int? id)
        {
            ViewData["roottypelist"] = RootNewsTypeEnum.支付方式.ToSelectListForAll();
            ViewData["newtype"] = NewsTypeEnum.入藏指南.ToSelectListForAll();
            var model = new SaveNews();

            if (id != null)
            {
                var item = _newsTask.GetById((int)id);
                if (item == null)
                    return AlertMsg("参数错误", HttpContext.Request.UrlReferrer.PathAndQuery);

                model = EntityMapper.Map<News, SaveNews>(item);
            }
            return View(model);
        }
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Save(SaveNews savemodel)
        {
            ViewData["roottypelist"] = RootNewsTypeEnum.购物指南.ToSelectListForAll();
            ViewData["newtype"] = NewsTypeEnum.入藏指南.ToSelectListForAll();
            if (ModelState.IsValid)
            {

                if (savemodel.ID == null)
                {
                    var model = new News
                                    {
                                        NewType = savemodel.NewType,
                                        Title = savemodel.Title,
                                        CreateDate = DateTime.Now,
                                        Content = savemodel.Content,
                                        ParentType = savemodel.ParentType,
                                        TypeName = _newsTypeTask.GetById(savemodel.NewType).TypeName,
                                        IsHot = savemodel.IsHot,
                                        ShortDes = savemodel.ShortDes,
                                    };
                    _newsTask.Add(model);
                }
                else
                {
                    var model = _newsTask.GetById((int)savemodel.ID);

                    if (model == null)
                        return AlertMsg("参数错误", HttpContext.Request.UrlReferrer.PathAndQuery);

                    model.NewType = savemodel.NewType;
                    model.Title = savemodel.Title;
                 
                    model.Content = savemodel.Content;
                    model.TypeName = _newsTypeTask.GetById(savemodel.NewType).TypeName;
                    model.ParentType = savemodel.ParentType;
                    model.IsHot = savemodel.IsHot;
                    model.ShortDes = savemodel.ShortDes;
                    _newsTask.Update(model);
                }
                return AlertMsg("保存成功", HttpContext.Request.UrlReferrer.PathAndQuery);

            }

            return View(savemodel);
        }



        #region 删除用户信息 Delete

        [HttpPost]
        public void Delete(int id)
        {
            _newsTask.Delete(id);
        }

        #endregion
    }
}